<?php
/**
 * gpa_new Description
 * @package
 * @name Config
 * @filesource Config.php
 * @author Meta Sanjaya
 */

class Config
{
    private $_config;
    private $db;
    /**
     * @access public
     * @method __construct
     * @param void
     * @return void
     */
    public function  __construct()
    {
        include_once __SYSTEM__ . 'configuration.php';
        $this->_config = $config;
        $this->db = Database::initialize($this->get('db_host'), $this->get('db_username'), $this->get('db_password'), $this->get('db_name'), $this->get('db_prefix'));
        $this->retrieve('global');
    }

    public function set($name, $value, $scope="")
    {
        $this->_config[$name] = $value;
        if(empty($scope)) {
            $scope = 'global';
        }

        $this->db->query("DELETE FROM #_settings WHERE `name`='$name'");
        $this->db->query("INSERT INTO #_settings SET `scope`='$scope', `name`='$name', `value`='$value'");
        $this->retrieve($scope);
    }

    public function get($name)
    {
        $return = false;

        foreach($this->_config as $config) {
            if(isset($config[$name])) {
                $return = $config[$name];
                break;
            }
        }

        return $return;
    }

    public function getAll()
    {
        return $this->_config;
    }

    public function retrieve($scope)
    {
        $rows = $this->db->query("SELECT scope, name, value FROM #_settings WHERE scope='$scope'")->rows;
        foreach($rows as $config) {
            if(!isset($this->_config[$config['scope']])) {
                $this->_config[$config['scope']] = array();
            }

            $this->_config[$config['scope']][$config['name']] = $config['value'];
        }
    }
}
?>